//用react封装
var currentpage = 1,totlepage = 15,i,selectMsgInfo = '';//分页的当前页和总的页数
//表单数据：用户id、提问人、手机号码、问题、最新回复时间、最后回复人
var tableData = [];
//表单的最高高度、当前第几页、总页数、总行数
var otherData = {};
//分页
var Page = React.createClass({
    setPage: function (e) {
        setPage($(e.target).html());
        this.props.onPageClick(1);
    },
    showPage: function () {
        showPage();
    },
    render: function () {
        {
            var items = [],
                svar = this;
            if(totlepage == 0){
                items.push({num:1,checked:true})
            }else if(totlepage < 6){
                for(i = 1;i <= totlepage ;i++) {
                    if (i == currentpage) {
                        items.push({num:i,checked:true})
                    } else {
                        items.push({num:i,checked:false})
                    }
                }
            }else if(currentpage >= (totlepage-2)) {//点在左边
                items.push({num:1,checked:false});
                items.push({num:'···',checked:false});
                for(i = totlepage-2;i <= totlepage ;i++){
                    if(i == currentpage){
                        items.push({num:i,checked:true})
                    }else{
                        items.push({num:i,checked:false})
                    }
                }
            }else if(currentpage <= 3){
                for(i = 1;i <= 3 ;i++) {
                    if (i == currentpage) {
                        items.push({num:i,checked:true})
                    } else {
                        items.push({num:i,checked:false})
                    }
                }
                items.push({num:'···',checked:false});
                items.push({num:totlepage,checked:false})
            }else {//点在两边
                items.push({num:1,checked:false});
                items.push({num:'···',checked:false});
                items.push({num:currentpage-1,checked:false});
                items.push({num:currentpage,checked:true});
                items.push({num:currentpage+1,checked:false});
                items.push({num:'···',checked:false});
                items.push({num:totlepage,checked:false})
            }
        }
        return (
            <div className="parentsInfo-page tableInfo-page">
                <div className="page-number">每页<span className="page-number-list" onClick={this.showPage}>{pagecount}</span>条</div>
                <div className="page-list" style={{top:pageListTop+"px"}}>
                    <ul>
                        {
                            pageList.map(function (item) {
                                return (<li onClick={svar.setPage}>{item}</li>)
                            })
                        }
                    </ul>
                </div>
                <div className="page-limit">
                    <ol className="page-limit-ol">
                        <li className="page-limit-img" name="-1" onClick={otherData.pageLeft == 0?'':svar.props.onPageClick.bind(svar,-1)} ><span className={otherData.pageLeft == 0?'page-limit-left-no':'page-limit-left-yes'}/></li>
                        {
                            items.map(function (item) {
                                return (<li onClick={isNaN(item.num)?'':svar.props.onPageClick.bind(svar,item.num)} className={item.checked == true?'page-limit-num page-limit-checked':'page-limit-num'} name={item.num}>{item.num}</li>)
                            })
                        }
                        <li className="page-limit-img" name="-2" onClick={otherData.pageRight == 0?'':svar.props.onPageClick.bind(svar,-2)}><span className={otherData.pageRight == 0?'page-limit-right-no':'page-limit-right-yes'}/></li>
                    </ol>
                </div>
            </div>
        );
    }
});
//表单组件
var Table = React.createClass({
    render: function () {
        return (
            <tr>
                <td style={{width: '5%'}}>{parseInt(this.props.listNumber)+1}</td>
                <td style={{width: '10%'}}>{this.props.peoplename}</td>
                <td style={{width: '15%'}}>{this.props.questiontime}</td>
                <td style={{width: '15%'}}>{this.props.peopletel}</td>
                <td style={{width: '15%'}}>{this.props.imei}</td>
                <td style={{width: '25%'}}>{this.props.content}</td>
                <td style={{width: '20%'}}>
                    <div className="table-select trButton trLeft" onClick={this.props.onClickSelect}>查看</div>
                    <div className="table-delete trButton trLeft" onClick={this.props.onClickDelete}>删除</div>
                </td>
            </tr>
        );
    }
});
/**
 * 问题详情弹出层
 * @type {*}
 */
var ProblemDetails = React.createClass({
    /**
     * 关闭弹出层
     */
    onClickHidePop: function () {
        $(".poplookMessage").hide();
    },
    render: function () {
        return (
            <div className="poplookMessage popModel">
                <div className="model"></div>
                <div className="poplookMessage-info popModel-info" style={{width:"570px",height:"auto",marginTop: "-249px"}}>
                    <div className="popModel-title">
                            <span className="popModel-title-content">
                                <span className="popModel-title-content-solid-left"/>
                                    反馈详情
                                <span className="popModel-title-content-solid-right"/>
                            </span>
                        <span className="popModel-title-img" onClick={this.onClickHidePop}><img src="../img/icon/close.png" alt="关闭"/></span>
                    </div>
                    <div className="popModel-body">
                        <input type="hidden" id="questionId"/>
                        <div className="popModel-body-text " >
                            <span className="popModel-body-text-title" >问题:</span>
                            <span className="popModel-body-text-content message-content msg-details" />
                        </div>
                        <div className="popModel-body-text" >
                            <span className="popModel-body-text-title">提问人:</span>
                            <span className="popModel-body-text-content poplookMessage-peopletel pop-username" />
                        </div>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >手机号:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-phone"/>
                        </div>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >设备IMEI号:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-imei"/>
                        </div>
                        <span className="popModel-body-title">用户使用环境</span>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >APP系统版本:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-phone-app-systemVersion"/>
                        </div>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >用户手机型号:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-phone-brand"/>
                        </div>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >手机系统版本:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-phone-systemVersion"/>
                        </div>
                        <span className="popModel-body-title">学生信息</span>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >所在学校:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-student-school"/>
                        </div>
                        <div className="popModel-body-text lookMessageTitle">
                            <span className="popModel-body-text-title" >所在班级:</span>
                            <span className="popModel-body-text-content poplookMessage-devicetype pop-student-class"/>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
});
//父组件
var Main = React.createClass({
    //组件初始状态
    getInitialState: function () {
        return {otherData:this.props.otherData,tableData:this.props.tableData}
    },
    componentDidMount: function () {
        //使用ajax取出数据然后组装数据列表
        var height = $(document.body).height()-326+"px";
        var svar = this;
        suixun.ajax(serverUrl+'/userQuestionManage/queryUserQuestionList.json?param={pagenumber:1,pagecount:"'+pagecount+'",status:"1"}','',"get",function(data){
            suixun.hideLoding();//隐藏加载中
            if(data.code == 200){
                for(var i=0;i<data.info.queryresult.length;i++){
                    var obj=data.info.queryresult[i];
                    tableData[i] = {keyid:i,questionid:obj.id,peoplename:obj.peoplename,peopletel:obj.peopletel,content:obj.content,questiontime:obj.questiontime,imei:obj.imei};
                }
                var pageRight = 0;
                if(data.info.totlepage > 1){
                    pageRight = 1
                }
                otherData = {maxHeight:height,pageLeft:0,pageRight:pageRight,gridOrmessage:0};
                currentpage = 1;totlepage = data.info.totlepage;//初始化的时候赋值：分页的当前页和总的页数
                svar.setState({tableData:tableData,otherData:otherData});
            }else{
                suixun.alert(data.msg);
                svar.msgNumber(0);//主页menu未处理的条数
                otherData = {maxHeight:height,pageLeft:0,pageRight:0,gridOrmessage:0};
                currentpage = 1;totlepage = 1;//初始化的时候赋值：分页的当前页和总的页数
                svar.setState({tableData:tableData,otherData:otherData});
            }
        });
        //绑定回车时间
        $(document).keypress(function(e) {
            var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
            if (eCode == 13){
                if($(".enterEvent").is(":focus")){
                    svar.onClickQuery();
                }
            }
        });
    },
    /**
     * 查询
     */
    onClickQuery: function () {
        var phone = $(".queryVal").val();
        if(phone.length > 0 && !/^[1-9]\d*$/.test(phone)){
            suixun.alert("输入框手机号格式错误！",function (data) {
                if(data){
                    $(".queryVal").val("");
                }
            });
            return;
        }
        suixun.showLoding();//显示加载中
        var param="param={";
        param = getJsonDataNotEmpty(param,"peopletel",'"'+phone+'"');
        param = getJsonDataNotEmpty(param,"pagenumber",'1');
        param = getJsonDataNotEmpty(param,"pagecount",pagecount+'');
        param += "}";
        var height = $(document.body).height()-326+"px",svar = this;
        suixun.ajax(serverUrl+'/userQuestionManage/queryUserQuestionList.json',param,"get",function(data){
            tableData = [];
            suixun.hideLoding();//隐藏加载中
            if(data.code == 200){
                for(var i=0;i<data.info.queryresult.length;i++){
                    var obj=data.info.queryresult[i];
                    tableData[i] = {keyid:i,questionid:obj.id,peoplename:obj.peoplename,peopletel:obj.peopletel,content:obj.content,questiontime:obj.questiontime,imei:obj.imei};
                }
                var pageRight = 0;
                if(data.info.totlepage > 1){
                    pageRight = 1
                }
                otherData = {maxHeight:height,pageLeft:0,pageRight:pageRight,gridOrmessage:0};
                currentpage = 1;totlepage = data.info.totlepage;//初始化的时候赋值：分页的当前页和总的页数
                svar.setState({tableData:tableData,otherData:otherData});
            }else{
                suixun.alert(data.msg,function (data) {
                    if(data){
                        otherData = {maxHeight:height,pageLeft:0,pageRight:0,gridOrmessage:0};
                        currentpage = 1;totlepage = 1;//初始化的时候赋值：分页的当前页和总的页数
                        svar.setState({tableData:tableData,otherData:otherData});
                    }
                });
            }
        });
    },
    /**
     * 分页
     * @param num
     */
    onPageClick: function (num) {
        //分页-调用ajac然后把数据查询出来:获取name的值
        //计算currentpage
        var onPageLeft = 0,onPageRight = 1,svar = this;
        if(num > 0){
            currentpage = num;
        }else if(num == -1 && currentpage > 1){
            currentpage --;
        }else if(num == -2  && currentpage < totlepage){
            currentpage ++;
        }
        //修改左右的按钮
        if((currentpage == totlepage && totlepage == 1)|| totlepage == 0){
            onPageLeft = 0;
            onPageRight = 0;
        }else if(currentpage == totlepage && currentpage > 1){
            onPageLeft = 1;
            onPageRight = 0;
        }else if(1 < currentpage && currentpage < totlepage){
            onPageLeft = 1;
            onPageRight = 1;
        }
        var phone = $(".queryVal").val();
        if(phone.length > 11){
            suixun.alert("输入框手机号格式错误！");
            return;
        }else if(phone.length > 0 && !/^[1-9]\d*$/.test(phone)){
            suixun.alert("输入框手机号格式错误！");
            return;
        }
        suixun.showLoding();//显示加载中
        //执行ajax获取数据然后组装
        var param="param={";
        param = getJsonDataNotEmpty(param,"peopletel",'"'+phone+'"');
        param = getJsonDataNotEmpty(param,"pagenumber",''+currentpage+'');
        param = getJsonDataNotEmpty(param,"pagecount",pagecount+'');
        param += "}";
        var height = $(document.body).height()-326+"px";
        suixun.ajax(serverUrl+'/userQuestionManage/queryUserQuestionList.json',param,"get",function(data){
            suixun.hideLoding();//隐藏加载中
            if(data.code == 200){
                tableData = [];
                for(var i=0;i<data.info.queryresult.length;i++){
                    var obj=data.info.queryresult[i];
                    tableData[i] = {keyid:i,questionid:obj.id,peoplename:obj.peoplename,peopletel:obj.peopletel,content:obj.content,questiontime:obj.questiontime,imei:obj.imei};
                }
                if(data.info.totlepage > 1 && data.info.totlepage > currentpage){
                    onPageRight = 1
                }else{
                    onPageRight = 0;
                }
                otherData = {maxHeight:height,pageLeft:onPageLeft,pageRight:onPageRight,gridOrmessage:0};
                totlepage = data.info.totlepage;//初始化的时候赋值：分页的当前页和总的页数
                svar.setState({tableData:tableData,otherData:otherData});
            }
        });
    },
    /**
     * 删除问题
     * @param id 回复id
     */
    onClickDelete: function(id){
        var svar = this;
        suixun.confirm_noTit('确定删除提问?',function (data) {
            if(data == true){
                var param="param={";
                param = getJsonDataNotEmpty(param,"id",""+id+"");
                param += "}";
                suixun.ajax(serverUrl+'/userQuestionManage/deleteUserQuestion.json?'+param,'',"get",function(data){
                    if(data.code == 200){
                        suixun.alert("删除成功!",function (data) {
                            if(data){
                                var tbodyData = $("#tbodyData").children();
                                if(tbodyData.length == 1 && currentpage > 1){
                                    totlepage = totlepage - 1;
                                    svar.onPageClick(currentpage-1);//更新数据
                                }else{
                                    svar.onPageClick(currentpage);//更新数据
                                }
                            }
                        })
                    }else {
                        suixun.alert(data.msg);
                    }
                });
            }
        });
    },

    /**
     * 查看反馈详情
     * @param questionid
     */
    onClickSelect: function (questionid) {
        suixun.showLoding();//显示加载中
        suixun.ajax(serverUrl+"/userQuestionManage/queryUserQuestion.json?param={id:\""+questionid+"\"}",'',"get",function(data){
            suixun.hideLoding();//隐藏加载中
            if(data.code == 200 ){
                if(data.info == null){
                    suixun.alert("数据错误!");
                    return;
                }
                $(".msg-details").html(data.info.content);//问题
                $(".pop-username").html(data.info.peoplename);//提问人姓名
                $(".pop-phone").html(data.info.peopletel);//手机号
                $(".pop-imei").html(data.info.imei);//设备imei号
                $(".pop-phone-app-systemVersion").html(data.info.appversion);//APP系统版本
                $(".pop-phone-brand").html(data.info.phonemodel);//用户手机型号
                $(".pop-phone-systemVersion").html(data.info.osversion);//手机系统版本
                $(".pop-student-school").html(data.info.schoolname);//所在学校
                $(".pop-student-class").html(data.info.classname);//所在班级
                $("#questionId").val(data.info.questionid);
                $(".poplookMessage").show();
            }else{
                suixun.alert(data.msg);
            }
        });
    },
    render: function () {
        {
            var svar = this;
            var onClickArray = [this.onClickSelect,this.onClickDelete];
            var Tabels = this.state.tableData.map(function (data,index) {
                return (
                    <Table key={data.keyid} questiontime={data.questiontime} imei={data.imei} listNumber={index} onClickSelect={onClickArray[0].bind(svar,[data.questionid])} onClickDelete={onClickArray[1].bind(svar,data.questionid)} peoplename={data.peoplename} peopletel={data.peopletel} content={data.content}  />
                )
            });
        }
        return (
            <div className="tableManage-body">
                <ProblemDetails onPageClick={this.onPageClick}/>
                <header>
                    <div className="header-left">
                        <div className="header-left-parentInfo" >
                            最新反馈
                        </div>
                        <div className="header-left-annotationsInfo">
                            <img src="../img/icon/annotations.png" />
                            <span className="header-left-annotations-arrowsLeft"/>
                            <div className="header-left-annotations">这里是注释。</div>
                        </div>
                    </div>
                    <div className="header-right">
                        <div className="header-right-query">
                            <div className="header-right-query-input">
                                <input className="queryVal enterEvent" type="text" maxLength="11" placeholder="请输入手机号码搜索"/>
                            </div>
                            <div className="header-right-query-img" onClick={this.onClickQuery}></div>
                        </div>
                    </div>
                </header>
                <div className="parentsInfo tableInfo">
                    <div className="parentsInfo-data tableInfo-data">
                        <div className="parentsInfo-data-table tableInfo-data-table" >
                            <table className="head-body head-body">
                                <thead>
                                    <th style={{width: '5%'}}>序号</th>
                                    <th style={{width: '10%'}}>提问人</th>
                                    <th style={{width: '15%'}}>提问时间</th>
                                    <th style={{width: '15%'}}>手机号码</th>
                                    <th style={{width: '15%'}}>设备IMEI号</th>
                                    <th style={{width: '25%'}}>问题</th>
                                    <th style={{width: '15%'}}>操作</th>
                                </thead>
                                <tbody id="tbodyData">
                                    {this.state.tableData==""?'':Tabels}
                                </tbody>
                            </table>
                            {this.state.tableData==""?<div className="tableNoData">{tableNoDataCount}</div>:''}
                        </div>
                    </div>
                    <Page otherData={this.state.otherData} onPageClick={this.onPageClick} />
                </div>
                <footer id="copyright">
                    <span><img src="../img/icon/copyright.png"/>{copyright}</span>
                </footer>
            </div>
        );
    }
});
ReactDOM.render(
    <Main  otherData={otherData} tableData={tableData} />,
    document.getElementById('tableManage')
);
$('.header-right .school-or-parent').hover(function(){
        if($(".drop-down").is(":hidden")){
            $(this).parent().find('.drop-down').slideDown();
            $(this).parent().hover(function(){},
                function(){
                    $(this).parent().find(".drop-down").slideUp();
                }
            );
        }
    },function(){}
);
$(".header-right-select").click(function () {
    var dropDown = $(this).find('.drop-down');
    if(dropDown.is(":hidden")){
        dropDown.slideDown();
    }else{
        dropDown.slideUp();
    }
});
$('.drop-down>span').click(function() {
    $(".queryVal").attr("placeholder","请输入家长"+$(this).html()+"搜索");
    $(".school-or-parent").html($(this).html());
    $('.drop-down').slideUp();
});